package com.koron.css2.clientCenter.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UnionInfoBean;
import com.koron.css2.clientCenter.bean.UnionInfoDetailBean;
import com.koron.css2.clientCenter.mapper.UnionInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

public class UnionInfoDelete implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "删除成功", String.class);
        try {
            UnionInfoBean unionInfoBean = JsonUtils.objectToPojo(req.getData(),UnionInfoBean.class);

            UnionInfoMapper unionInfoMapper = factory.getMapper(UnionInfoMapper.class);

            if (ObjectUtil.isEmpty(unionInfoBean)) {
                return  MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败,入参不能为空", null);
            }

            //根据 union_no 查询 user_info数据
            List<String> userNoList = unionInfoMapper.userNoQueryList(unionInfoBean.getUnionNo());
            //置空 user_info
            if (CollectionUtil.isNotEmpty(userNoList)) {
                UnionInfoDetailBean detailBean = new UnionInfoDetailBean();
                detailBean.setUnionNo(null);
                detailBean.setUserNoList(userNoList);
                unionInfoMapper.unionInfoDeatilUpdate(detailBean);
            }
            //user_info置空后，删除 union_info 表数据
            unionInfoMapper.delete(unionInfoBean);

            return info;
        }catch (Exception e) {
            logger.error("联合用户保存失败" + e.getMessage(), e);
            return  MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
        }
    }
}
